package com.wswy.wzcx.utils;

import android.Manifest;
import android.Manifest.permission;
import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.telephony.TelephonyManager;

import com.blankj.utilcode.util.CacheDiskUtils;
import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.NetworkUtils.NetworkType;
import com.blankj.utilcode.util.PermissionUtils;
import com.che.updater.SpUtils;
import com.wswy.wzcx.AppContext;
import com.wswy.wzcx.aanewApi.baseUtils.Toast;

import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;

public class Tools {

    private static CacheDiskUtils sApiCache = null;
    private static CacheDiskUtils sDefaultCache = null;

    public static CacheDiskUtils getCache() {
        if (sDefaultCache == null) {
            synchronized (Tools.class) {
                if (sDefaultCache == null) {
                    sDefaultCache = CacheDiskUtils.getInstance();
                }
            }
        }
        return sDefaultCache;
    }


    public static CacheDiskUtils getApiCache() {
        if (sApiCache == null) {
            synchronized (Tools.class) {
                if (sApiCache == null) {
                    sApiCache = CacheDiskUtils.getInstance("api_cache");
                }
            }
        }
        return sApiCache;
    }

    public static SpUtils getSp() {
        return SpUtils.getInstance(AppContext.getContext());
    }

    public static int evaluateColor(int startInt, int endInt, float fraction) {
        float startA = ((startInt >> 24) & 0xff) / 255.0f;
        float startR = ((startInt >> 16) & 0xff) / 255.0f;
        float startG = ((startInt >> 8) & 0xff) / 255.0f;
        float startB = (startInt & 0xff) / 255.0f;

        float endA = ((endInt >> 24) & 0xff) / 255.0f;
        float endR = ((endInt >> 16) & 0xff) / 255.0f;
        float endG = ((endInt >> 8) & 0xff) / 255.0f;
        float endB = (endInt & 0xff) / 255.0f;

        // convert from sRGB to linear
        startR = (float) Math.pow(startR, 2.2);
        startG = (float) Math.pow(startG, 2.2);
        startB = (float) Math.pow(startB, 2.2);

        endR = (float) Math.pow(endR, 2.2);
        endG = (float) Math.pow(endG, 2.2);
        endB = (float) Math.pow(endB, 2.2);

        // compute the interpolated color in linear space
        float a = startA + fraction * (endA - startA);
        float r = startR + fraction * (endR - startR);
        float g = startG + fraction * (endG - startG);
        float b = startB + fraction * (endB - startB);

        // convert back to sRGB in the [0..255] range
        a = a * 255.0f;
        r = (float) Math.pow(r, 1.0 / 2.2) * 255.0f;
        g = (float) Math.pow(g, 1.0 / 2.2) * 255.0f;
        b = (float) Math.pow(b, 1.0 / 2.2) * 255.0f;

        return Math.round(a) << 24 | Math.round(r) << 16 | Math.round(g) << 8 | Math.round(b);
    }


    public static void startCallPhone(Context context, String phone) {
        try {
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    private static String sIMEI = null;
    private static String sPhoneNumber = null;

    @SuppressLint("MissingPermission")
    public static String getIMEI() {

        if (sIMEI == null && PermissionUtils.isGranted(permission.READ_PHONE_STATE)) {
            try {
                TelephonyManager telephonyManager = (TelephonyManager) AppContext.getContext().getSystemService(Context.TELEPHONY_SERVICE);
                String imei = telephonyManager.getDeviceId();
                if (imei != null) {
                    sIMEI = imei;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return sIMEI;

    }

    @SuppressLint("MissingPermission")
    public static String getPhoneNumber() {
        if (sPhoneNumber == null && PermissionUtils.isGranted(permission.READ_PHONE_STATE)) {
            try {
                TelephonyManager telephonyManager = (TelephonyManager) AppContext.getContext().getSystemService(Context.TELEPHONY_SERVICE);
                String phone = telephonyManager.getLine1Number();
                if (phone != null) {
                    sPhoneNumber = phone;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return sPhoneNumber;
    }

    public static String getNetWork() {
        NetworkType networkType = NetworkUtils.getNetworkType();
        switch (networkType) {
            case NETWORK_ETHERNET:
                return "ethernet";
            case NETWORK_2G:
                return "2G";
            case NETWORK_3G:
                return "3G";
            case NETWORK_4G:
                return "4G";
            case NETWORK_WIFI:
                return "WIFI";
            case NETWORK_NO:
                return "NO";
            default:
                return "UNKNOWN";
        }


    }

    public static boolean isVpnUsed() {
        try {
            Enumeration<NetworkInterface> niList = NetworkInterface.getNetworkInterfaces();
            if (niList != null) {
                ArrayList<NetworkInterface> list = Collections.list(niList);
                for (NetworkInterface intf : list) {
                    if (!intf.isUp() || intf.getInterfaceAddresses().size() == 0) {
                        continue;
                    }
                    String name = intf.getName();
                    if ("tun0".equals(name) || "ppp0".equals(name)) {
                        return true; // The VPN is up
                    }
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return false;
    }


    public static void copy(Context context, String content) {
        //获取剪贴板管理器：
        ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建普通字符型ClipData
        ClipData mClipData = ClipData.newPlainText("Label", content);
        // 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData);
        Toast.show("已复制,即将跳转到微信");
    }

}
